package com.itboye.ihomebank.activity.me;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.itboye.ihomebank.R;
import com.itboye.ihomebank.base.BaseOtherActivity;
import com.itboye.ihomebank.bean.ResultEntity;
import com.itboye.ihomebank.constants.SPContants;
import com.itboye.ihomebank.presenter.UserPresenter;
import com.itboye.ihomebank.util.ByAlert;
import com.itboye.ihomebank.util.SPUtils;

import java.util.Observable;
import java.util.Observer;
/**
 * 修改昵称
 *
 */
public class ActivityNiCheng extends BaseOtherActivity implements OnClickListener,Observer{
	View v_statusbar;
	private ImageView close_icon;//返回图片
	private TextView add_shap_title_tv;//标题
	EditText nicheng_et;//昵称
	TextView nicheng_ok;//完成
	
	private UserPresenter userPresenter;
	private String uid;//id
	private String nick;//nickname

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.close_icon:
			finish();
			break;
		case R.id.nicheng_ok:

			if (nicheng_et.getText().toString().equals(null)
					&&nicheng_et.getText().toString()=="") {
				ByAlert.alert("昵称不能为空");
				return;
			} else if (nicheng_et.getText().length()>16){
				ByAlert.alert("昵称不能超过16位请修改");
			}else {
				showProgressDialog("请稍后", false);
				setNickName();
			}
			break;

		default:
			break;
		}
		
	}
	/**
	 * 修改昵称
	 */
	private void setNickName(){
		userPresenter=new UserPresenter(this);
		userPresenter.updateInfo(uid
				, SPUtils.get(ActivityNiCheng.this, null, SPContants.RealName, "") + ""
				,SPUtils.get(ActivityNiCheng.this, null, SPContants.EMAIL, "") + ""
				,SPUtils.get(ActivityNiCheng.this, null, SPContants.Birthday, "") + ""
				,nicheng_et.getText().toString()
				,SPUtils.get(ActivityNiCheng.this, null, SPContants.SEX, "") + ""
				,SPUtils.get(ActivityNiCheng.this, null, SPContants.QQ, "") + ""
				,SPUtils.get(ActivityNiCheng.this, null, SPContants.SIGN, "") + "");
	}

	@Override
	public void update(Observable observable, Object data) {
		ResultEntity entity=handlerError(data);
		if(entity!=null){
			if(!entity.getCode().equals(0)){
				if (entity.getEventType() == UserPresenter.updateUser_success) {
					String bean = (String) entity.getData();
					if(bean!=null){
						ByAlert.alert("修改成功");
						SPUtils.put(ActivityNiCheng.this, null, SPContants.NICK, nicheng_et.getText().toString());
						Intent intent = new Intent(ActivityNiCheng.this,
								ActivityMyData.class);
						intent.putExtra("nickname", nicheng_et.getText().toString());
						setResult(RESULT_OK, intent);
						ActivityNiCheng.this.finish();
					}
					
				}else if(entity.getEventType() == UserPresenter.updateUser_fail){
					ByAlert.alert("修改失败");
				}
			}
		}
		try {
			closeProgressDialog();
		} catch (Exception e) {
		}
		
	}

	@Override
	protected int layout_id() {
		// TODO Auto-generated method stub
		return R.layout.activity_ni_cheng;
	}

	@Override
	protected void onInitController() {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected void onInit() {
		setStatusBarHeight(v_statusbar);
		add_shap_title_tv.setText("修改昵称");
		nick=getIntent().getStringExtra("nick");
		if (nick.equals("")||nick==null){
			nicheng_et.setText("");
		}else {
			nicheng_et.setText(nick);
			nicheng_et.setSelection(nick.length());
		}
		uid=(String)SPUtils.get(ActivityNiCheng.this, null, SPContants.USER_ID, "");
	    nicheng_et.addTextChangedListener(new TextWatcher() {
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {
				if (start+after>16){
					Toast.makeText(ActivityNiCheng.this,"昵称最长16个字哦",Toast.LENGTH_SHORT).show();
//					nicheng_et.setText(s.toString().substring(0,16));
				}
			}

			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
//				ByAlert.alert(start+"=="+before+"=="+count);
			}

			@Override
			public void afterTextChanged(Editable s) {
				if (s.toString().length()>16){
					nicheng_et.setText(s.toString().substring(0,16));
					nicheng_et.setSelection(16);
				}
			}
		});
	}

	@Override
	protected void onReadIntent(Bundle bundle) {
		// TODO Auto-generated method stub
		
	}

/*	private void setNickName() {//修改昵称
		MyJsonRequest<String> request = new MyJsonRequest.Builder<String>()
				.apiVer("100")
				.typeKey("BY_User_update")
				.param("uid",
						(String) SPUtils.get(ActivityNiCheng.this, null, SPContants.USER_ID,
								""))
				.param("nickname", nicheng_et.getText().toString())
				.requestListener(new XRequestListener<String>() {

					@Override
					public void onResponse(String response) {
						DebugLog.v("result", response.toString());
						T.showShort(ActivityNiCheng.this, "修改成功");
						SPUtils.put(ActivityNiCheng.this, null, SPContants.NICK, nicheng_et
								.getText().toString());

						Intent intent = new Intent(ActivityNiCheng.this,
								ActivityMyData.class);
						intent.putExtra("nickname", nicheng_et.getText()
								.toString());
						setResult(RESULT_OK, intent);
						ActivityNiCheng.this.finish();
					}
				}).errorListener(new XErrorListener() {

					@Override
					public void onErrorResponse(Exception exception, int code,
							String msg) {
						if (exception instanceof CodeErrorException) {
							T.showShort(ActivityNiCheng.this, "网络异常");
						} else {
							T.showShort(ActivityNiCheng.this, "网络异常");
						}

					}
				}).build();
		HttpRequest.getDefaultRequestQueue().add(request);

	}*/

}
